package com.contacts.mapper;

import com.contacts.pojo.Category;
import com.contacts.pojo.Contact;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ContactMapper {
    /**
     * 根据ID删除联系人
     * @param id
     */
    void deleteContactById(Integer id);

    /**
     * 添加联系人
     * @param contact
     * @param userIdFromToken
     */
    @Insert("insert into contacts(ct_name, ct_gender, ct_phone, ct_email, user_id, category) values (#{contact.ctName},#{contact.ctGender},#{contact.ctPhone},#{contact.ctEmail},#{userIdFromToken},#{contact.category})")
    void insertContact(Contact contact,Integer userIdFromToken);

    /**
     * 修改联系人
     * @param contact
     * @param userIdFromToken
     */
    void updateContact(Contact contact, Integer userIdFromToken);

    /**
     * 根据ID查询联系人
     * @param id
     * @return
     */
    @Select("select ct_id,ct_name,ct_gender,ct_phone,ct_email,category from contacts where ct_id = #{id}")
    Contact findById(Integer id);

    /**
     * 联系人统计分析
     * @param id
     * @return
     */
    @Select("select category,count(*) as total from contacts where user_id=#{id} group by category")
    List<Category> getTotalById(Long id);
}
